/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import xmlutils.PlayerUtils;

/**
 *
 * @author TungNT60513
 */
public class PlayerServlet extends HttpServlet {

    private final String playerPage = "player.jsp";

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        XMLStreamReader xsr = null;
        InputStream is = null;

        try {
            String action = request.getParameter("action");
            String idSong = request.getParameter("songID");
            String idArtist = request.getParameter("artistID");
            PlayerUtils pu = new PlayerUtils();

            if (action.equals("Like")) {
                String realPath = getServletContext().getRealPath("/");
                String filePath = realPath + "xml/Songs.xml";

                pu.likeControl(idSong, filePath); // goi ham likeControl trong PlayerUtils

                RequestDispatcher rd = request.getRequestDispatcher(playerPage); //return playerPage
                rd.forward(request, response);
            } else if (action.equals("Unlike")) {
                String realPath = getServletContext().getRealPath("/");
                String filePath = realPath + "xml/Songs.xml";

                pu.unlikeControl(idSong, filePath); // goi ham unlikeControl trong PlayerUtils

                RequestDispatcher rd1 = request.getRequestDispatcher(playerPage); //return playerPage
                rd1.forward(request, response);
            } else if (action.equals("Subscribe")) {
                String realPath = getServletContext().getRealPath("/");
                String filePath = realPath + "xml/Artists.xml";

                pu.subscribeControl(idArtist, filePath); // goi ham subscribeControl

                RequestDispatcher rd = request.getRequestDispatcher(playerPage); // return playerPage
                rd.forward(request, response);
            } else if (action.equals("Unsubscribe")) {
                String realPath = getServletContext().getRealPath("/");
                String filePath = realPath + "xml/Artists.xml";

                pu.unsubscribeControl(idArtist, filePath); // goi ham unsubscribeControl

                RequestDispatcher rd = request.getRequestDispatcher(playerPage);
                rd.forward(request, response);
            }
        } finally {
            out.close();
            try {
                if (is != null) {
                    is.close();
                }
                if (xsr != null) {
                    xsr.close();
                }
            } catch (IOException ex) {
                System.out.println("IOException: " + ex);
            } catch (XMLStreamException ex) {
                System.out.println("XML StreamException " + ex);
            }
        } // end finally
    } // end processRequest

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
